<!DOCTYPE html>
<html>

<head>
    <meta charset='utf-8' />
    <title>Measure distances</title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <script include="jquery" src="./static/libs/include-lib-local.js"></script>
    <script include="geohash,geojson,turf" src="./static/libs/include-cesium-local.js"></script>
    <!--当前示例页面样式表引用-->
    <link rel="stylesheet" href="./static/demo/cesium/style.css" />
</head>

<body>

    <div id='GlobeView'></div>
    <script>
        var webGlobe, map;
        var geojson;

        initMap();
        loadData();

        function initMap() {
            webGlobe = new Cesium.WebSceneControl('GlobeView', {
                terrainExaggeration: 1,
            });

            map = webGlobe.viewer;

            //构造第三方图层对象
            var thirdPartyLayer = new CesiumZondy.Layer.ThirdPartyLayer({
                viewer: webGlobe.viewer
            });
            //加载天地图
            var tdtLayer = thirdPartyLayer.appendTDTuMap({
                //天地图经纬度数据
                url: 'http://t0.tianditu.com/DataServer?T=vec_c&X={x}&Y={y}&L={l}',
                //开发token （请到天地图官网申请自己的开发token，自带token仅做功能验证随时可能失效）
                token: "9c157e9585486c02edf817d2ecbc7752",
                //地图类型 'vec'矢量 'img'影像 'ter'地形
                ptype: "img"
            });

            var center = Cesium.Cartesian3.fromDegrees(116.35, 39.9, 50000.0);
            map.scene.camera.setView({
                destination: center
            });
        }

        function loadData() {
            $.getJSON('./static/data/client-analysis/buffer-hash-4.json', function(data) {
                convertDataToGeoJson(data);
                updateView(geojson);
            });
        }

        function convertDataToGeoJson(origindata) {
            var columnarPoints = [];
            var points;
            origindata.aggregations.geohash.buckets.forEach(function(bucket) {
                var coordinates = decodeGeoHash(bucket.key);
                var countNumber = bucket.doc_count;
                var point = {
                    pointKey: [
                        coordinates.longitude[2], coordinates.latitude[2]
                    ],
                    count: bucket.doc_count
                }; //[0] min [1]max [2] 中心点
                columnarPoints.push(point);
            });
            points = GeoJSON.parse(columnarPoints, {
                'Point': 'pointKey'
            });
            geojson = turf.centroid(points);
            var origindatasource = map.dataSources.add(Cesium.GeoJsonDataSource.load(points, {
                markerColor: Cesium.Color.GRAY,
                markerSize: 50
            }));
            map.flyTo(origindatasource);
        }

        function updateView(data) {
            var centerdatasource = map.dataSources.add(Cesium.GeoJsonDataSource.load(data, {
                markerColor: Cesium.Color.RED,
                markerSize: 50
            }));
        }
    </script>

</body>

</html>